class com.nesium.utils.ColorUtils
	// v1.10
{


	public static function hexToRgb( hex : Number ) : Object
	{
		var clr : Object = 
		{
			r	: ( hex >> 16 ) & 0xFF,
			g	: ( hex >> 8 ) & 0xFF,
			b	: hex & 0xFF
		}
		return clr;
	}



	public static function rgbToHex( r : Number, g : Number, b : Number ) : Number
	{
		return ( r << 16 ) | ( g << 8 ) | b;
	}



	public static function hexToString( hex : Number, addHash : Boolean ) : String
	{
		var hexString : String 	= addHash != false ? "#" : "";
		hexString += hex.toString( 16 );	
		return hexString;
	}
	
	
	
	public static function hexStringToNumber( hexStr : String ) : Number
	{
		return Number( "0x" + hexStr.slice( 1 ) );
	}
		
	
	
	public static function transformColor( col : Number, dif : Number ) : Number
	{
		var c = hexToRgb( col );
		for ( var val in c )
		{
			c[ val ] += dif;
			if ( c[ val ] < 0 ) c[ val ] = 0;
			else if ( c[ val ] > 255 ) c[ val ] = 255;
		}
		return rgbToHex( c.r, c.g, c.b );
	}
	
	
	public static function colorMc( mc : MovieClip, col : Number, alpha : Number ) : Color
	{
		var c 	: Color = new Color( mc );
		var rgb	: Object = hexToRgb( col );
		c.setTransform( { ra : alpha, rb : rgb.r, ga : alpha, gb : rgb.g, ba : alpha, bb : rgb.b, aa : 100, ab : 0 } );
		return c;
	}
	
	
	public static function changeColor( c : Color, col : Number, alpha : Number ) : Void
	{
		var rgb	: Object = hexToRgb( col );
		c.setTransform( { ra : alpha, rb : rgb.r, ga : alpha, gb : rgb.g, ba : alpha, bb : rgb.b, aa : 100, ab : 0 } );
	} 
	
	
	
	public static function hsbToHex( h : Number, s : Number, br : Number ) : Number
	{
		var r : Number;
		var g : Number;
		var b : Number;
		var rgb : Number;
	
		if (s != undefined) {
			s = (100 - s ) / 100;
			br = ( 100 - br ) / 100;
		}
	
		if ( ( h  > 300 && h <= 360 ) || ( h >= 0 && h <= 60 ) ) 
		{
			r = 255;
			g = ( h / 60 ) * 255;
			b = ( ( 360 - h ) / 60 ) * 255;
		} 
		else if ( h > 60 && h <= 180 ) 
		{
			r = ( ( 120 - h ) / 60 ) * 255;
			g = 255;
			b = ( ( h - 120 ) / 60 ) * 255;
		} 
		else 
		{
			r = ( ( h - 240 ) / 60 ) * 255;
			g = ( ( 240 - h ) / 60 ) * 255;
			b = 255;
		}
		
		if ( r > 255 || r < 0 ) r = 0;
		if ( g > 255 || g < 0 ) g = 0;
		if ( b > 255 || b < 0 ) b = 0;
		
		if ( s != undefined ) 
		{	
			r += ( 255 - r ) * s;
			g += ( 255 - g ) * s;
			b += ( 255 - b ) * s;
			r -= r * br;
			g -= g * br;
			b -= b * br;
			r = Math.round( r );
			g = Math.round( g );
			b = Math.round( b );
		}
		rgb = b + ( g * 256 ) + ( r * 65536 );
		return rgb;
	}
	
	
	
        public static function hexToHsb( hex : Number ) : Object 
        {
		var rgb : Object = hexToRgb( hex );
		var r : Number = rgb.r;
		var g : Number = rgb.g;
		var b : Number = rgb.b;
		
		var hsb : Object = {};
		hsb.b = Math.max( Math.max( r, g ), b );
		var min = Math.min( r, Math.min( g, b ) );
		hsb.s = ( hsb.b <= 0 ) ? 0 : Math.round( 100 * ( hsb.b - min ) / hsb.b );
		hsb.b = Math.round( ( hsb.b / 255 ) * 100 );
                	hsb.h = 0;
                
                	if ( ( r == g ) && ( g == b ) )
			hsb.h = 0;
                	else if ( r >= g && g >= b )
			hsb.h = 60 * ( g - b ) / ( r - b );
                	else if ( g >= r && r >= b )
                        hsb.h = 60 + 60 * ( g - r ) / ( g - b );
                	else if ( g >= b && b >= r )
                        hsb.h = 120 + 60 * ( b - r ) / ( g - r );
                	else if ( b >= g && g >= r)
                        hsb.h = 180 + 60 * ( b - g ) / ( b - r );
                	else if ( b >= r && r >=  g )
                        hsb.h = 240 + 60 * ( r - g ) / ( b - g );
                	else if ( r >= b && b >= g )
                        hsb.h = 300 + 60 * ( r - b ) / ( r - g );
                	else
                        hsb.h = 0;

                hsb.h = Math.round( hsb.h );
                return hsb;
        }
}